package com.dhcc.springboot.test.controller;

import com.dhcc.springboot.test.bean.User;
import com.dhcc.springboot.test.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @program: test
 * @description: 控制层：user相关 postman测试
 * @author: liu denghui
 * @create: 2019/08/13 09:55
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 根据id查询用户
     */
    @RequestMapping(value = "/queryById" ,method = RequestMethod.GET)
    public User queryById(Integer id){
        User user = userService.selectByPrimaryKey(id);
        return user;
    }

    /**
     * 查询用户
     * 根据姓名（可模糊查询），性别，年龄，成绩
     */
    @RequestMapping(value = "/queryByUser" ,method = RequestMethod.POST)
    public List<User> queryByUser(@RequestBody User user){
        List<User> users = new ArrayList<>();
        users = userService.queryByUser(user);
        return users;
    }

    /**
     * 新增用户
     */
    @RequestMapping(value = "/insertUser" ,method = RequestMethod.POST)
    public int insertUser(@RequestBody User user){
        return userService.insert(user);
    }

    /**
     * 删除用户（可批量）
     * 前台将需要删除的id组合成字符串：1,2,3
     */
    @RequestMapping(value = "/deleteUsers" ,method = RequestMethod.POST)
    public int deleteUsers(String ids){
        return userService.deleteByPrimaryKeys(ids);
    }

    /**
     * 修改用户（单个）
     */
    @RequestMapping(value = "/updateUser" ,method = RequestMethod.POST)
    public int updateUser(@RequestBody User newUser){
        return userService.updateByPrimaryKey(newUser);
    }
}
